import { _decorator, Collider2D, Component, Contact2DType, ICollisionEvent, IPhysics2DContact, Node, Vec3 } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('PeaController')
export class PeaController extends Component {
    @property(Number)
    speed: number = 100;
    start() {
        // 注册单个碰撞体的回调函数
        let collider = this.getComponent(Collider2D);
        if (collider) {
            collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
        }
    }
    onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
        // 只在两个碰撞体开始接触时被调用一次
        console.log('onBeginContact');
        this.node.destroy();
    }
    update(deltaTime: number) {
        this.node.setPosition(new Vec3(this.node.position.x + this.speed * deltaTime, this.node.position.y, this.node.position.z))
    }
}

